<template>
  <div v-if="good">
    <p class="g-name">{{ good.name }}</p>
    <p class="g-desc">{{ good.desc }}</p>
    <p class="g-price">
      <span>{{ good.price }}</span>
      <span>{{ good.oldPrice }}</span>
    </p>
    <div class="g-service">
      <dl>
        <dt>促销</dt>
        <dd>12月好物放送，App领券购买直降120元</dd>
      </dl>
      <dl>
        <dt>配送</dt>
        <dd>至 <xtx-city @change-address="changeAddressHandler" :fullLocation="fullLocation"></xtx-city></dd>
      </dl>
      <dl>
        <dt>服务</dt>
        <dd>
          <span>无忧退货</span>
          <span>快速退款</span>
          <span>免费包邮</span>
          <a href="javascript:;">了解详情</a>
        </dd>
      </dl>
    </div>
  </div>
</template>

<script setup>
import { defineProps, watch, ref } from 'vue'
const props = defineProps({
  goods: {
    type: Object,
    default: () => ({})
  }
})
const good = ref(null)
watch(() => props.goods, newVal => {
  good.value = newVal
}, { immediate: true })

// 经过分析:1.城市组件将来需要传递出来的数据包括：省code、市code、区code,省市区名称的字符串;2.如果当前用户是登录状态且有默认收货地址，那么城市组件就显示该默认值;3.如果没有默认地址，就显示 北京市 市辖区 东城区
// 假设用户没有默认收货地址
const provinceCode = ref('110000')
const cityCode = ref('119900')
const countyCode = ref('110101')
const fullLocation = ref('北京市 市辖区 东城区')
// 有默认地址
if (props.goods.userAddresses) {
  const defaultAddr = props.goods.userAddresses.find(addr => addr.isDefault === 1)
  if (defaultAddr) {
    provinceCode.value = defaultAddr.provinceCode
    cityCode.value = defaultAddr.cityCode
    countyCode.value = defaultAddr.countyCode
    fullLocation.value = defaultAddr.fullLocation
  }
}
// 修改省市区数据
function changeAddressHandler(changeResult) {
  provinceCode.value = changeResult.provinceCode
  cityCode.value = changeResult.cityCode
  countyCode.value = changeResult.countyCode
  fullLocation.value = changeResult.fullLocation
}

</script>

<style lang="less" scoped>
.g-name {
  font-size: 22px
}

.g-desc {
  color: #999;
  margin-top: 10px;
}

.g-price {
  margin-top: 10px;

  span {
    &::before {
      content: "¥";
      font-size: 14px;
    }

    &:first-child {
      color: @priceColor;
      margin-right: 10px;
      font-size: 22px;
    }

    &:last-child {
      color: #999;
      text-decoration: line-through;
      font-size: 16px;
    }
  }
}

.g-service {
  background: #f5f5f5;
  width: 500px;
  padding: 20px 10px 0 10px;
  margin-top: 10px;

  dl {
    padding-bottom: 20px;
    display: flex;
    align-items: center;

    dt {
      width: 50px;
      color: #999;
    }

    dd {
      color: #666;

      &:last-child {
        span {
          margin-right: 10px;

          &::before {
            content: "•";
            color: @xtxColor;
            margin-right: 2px;
          }
        }

        a {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>